/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-7-11 上午9:41 Created by sky
 */
package com.jade.modules.message.sms;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 短信操作结果。
 *
 * @author sky
 * @version 1.0 13-7-11 上午9:41
 */
public class SmsResult
        implements Serializable
{
    protected int[] codes;
    protected String[] messages;
    protected String[] msgIds;

    public SmsResult(int count)
    {
        this(count, null);
    }

    public SmsResult(int count, String msgId)
    {
        this(count, msgId, SmsStatus.SC_OK, null);
    }

    public SmsResult(int count, int code, String message)
    {
        this(count, null, code, message);
    }

    public SmsResult(int count, String msgId, int code, String message)
    {
        codes = new int[count];
        messages = new String[count];
        msgIds = new String[count];
        Arrays.fill(msgIds, msgId);
        Arrays.fill(codes, code);
        Arrays.fill(messages, message);
    }

    public SmsResult(String msgId, int code, String message)
    {
        this(1, msgId, code, message);
    }

    public SmsResult(String msgId, int[] codes, String[] messages)
    {
        msgIds = new String[codes.length];
        Arrays.fill(msgIds, msgId);
        this.codes = codes;
        this.messages = messages;
    }

    public void setResult(int index, String msgId, int code, String message)
    {
        msgIds[index] = msgId;
        codes[index] = code;
        messages[index] = message;
    }

    public int getCode(int index)
    {
        return codes[index];
    }

    public String getMessage(int index)
    {
        return messages[index];
    }

    public String getMsgId(int index)
    {
        return msgIds[index];
    }

    public String toString()
    {
        if (codes == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < codes.length; i++) {
            sb.append(codes[i]).append(":");
            if (codes[i] == SmsStatus.SC_NETWORK_ERROR) {
                sb.append(msgIds[i]);
            }
            else {
                sb.append(messages[i]);
            }
            sb.append(";");
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * 判断消息结果是否为成功。
     *
     * @param index 顺序号
     * @return 成功返回true；否则返回false.
     */
    public boolean isOK(int index)
    {
        return SmsStatus.SC_OK == codes[index];
    }
}
